
import { _decorator, Component, Node, primitives, utils, MeshRenderer, Material, color, randomRange } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('HexMesh')
export class HexMesh extends Component {

    public static outer_radius = 1;

    material: Material;
    init(mat:Material) {
        this.material = mat;
    }
    onLoad() {
        let outer_radius = HexMesh.outer_radius;
        let inner_radius = outer_radius * 0.866025404;
        let vers = [
            0, 0, 0,
            0, 0, outer_radius,
            inner_radius, 0, 0.5 * outer_radius,
            inner_radius, 0, -0.5 * outer_radius,
            0, 0, -outer_radius,
            -inner_radius, 0, -0.5 * outer_radius,
            -inner_radius, 0, 0.5 * outer_radius
        ];
        let triangles: number[] = [0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 1];
        let hexmap: primitives.IGeometry = {
            positions: vers,
            indices: triangles
        };
        let mesh = utils.createMesh(hexmap);

        let mr = this.addComponent(MeshRenderer);
        mr.mesh = mesh;
        mr?.setMaterial(this.material,0);
    }
}
